package demo.jdk.features.jdk7;

import java.io.IOException;
import java.nio.file.*;

/**
 * @author Ranchoz  2018/9/9 15:48
 **/
public class WatchAndCalculate {
	public void calculate() throws IOException, InterruptedException {
		WatchService service = FileSystems.getDefault().newWatchService();
		Path path = Paths.get("").toAbsolutePath();
		path.register(service, StandardWatchEventKinds.ENTRY_CREATE);
		while (true) {
			WatchKey key = service.take();
			System.out.println("------------------------------");
			for (WatchEvent<?> event : key.pollEvents()) {
				Path createdPath = (Path) event.context();
				createdPath = path.resolve(createdPath);
				long size = Files.size(createdPath);
				System.out.println(createdPath + " ==> " + size);
			}
			key.reset();
		}
	}
	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) throws Throwable {
		WatchAndCalculate wc = new WatchAndCalculate();
		wc.calculate();
	}
}
